/**
 * Copyright (c) 2017 hadlinks, All Rights Reserved.
 */
package com.bugull.farm.core.control.example;

import com.bugull.farm.core.control.AbstractBuguEventListener;
import com.bugull.farm.core.enums.CommandEnum;
import com.bugull.farm.core.server.FarmServer;
import com.bugull.mongo.cache.DaoCache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Project Name: bugu-farm-household
 * Package Name: com.bugull.farm.core.example
 * ClassName: CmdResEventListener
 * Function: 短连接设备设置参数设置命令响应监听器.
 * date: 2017/10/27 18:18
 * @author songwei (songw@hadlinks.com)
 * @since JDK 1.8 
 */
public class CmdResEventListener extends AbstractBuguEventListener<CmdResEvent> {

    private Logger logger = LogManager.getLogger(CmdResEventListener.class);

    @Override
    public void fireEvent(CmdResEvent event) {

        switch (event.getType()) {
            case EVENT_TYPE_CMD_RESPONSE:
                dealRes(event);
                break;
            case EVENT_TYPE_CMD_TIMEOUT:
                dealTimeout(event);
                break;
            case EVENT_TYPE_CMD_EXCEPTION:
                dealException(event);
                break;
            default:
                logger.debug("无法识别的事件类型");
        }
        //TODO 完成具体的事件触发后的操作
        if (event.getCmd() == CommandEnum.CMD_DEV_SHORTCONN_TEMP_HUM_VOLTAGE_SET) {

        }
    }

    public void dealRes(CmdResEvent event) {
        //检查是否有离线任务，有任务继续执行任务
//        BuguDao<CommandTask> ctDao = DaoCache.getInstance().get(CommandTask.class);
//        BuguQuery<CommandTask> q1 = ctDao.query().notEquals("tempComplete", Boolean.TRUE);
//        BuguQuery<CommandTask> q2 = ctDao.query().notEquals("positionComplete", Boolean.TRUE);
//        BuguQuery<CommandTask> q3 = ctDao.query().notEquals("humComplete", Boolean.TRUE);
//        CommandTask commandTask = ctDao.query().is("terminalSN", event.getTerminalSN()).or(q1, q2, q3).result();
//        switch (event.getCmd()) {
//            case CMD_DEV_SHORTCONN_TEMP_HUM_VOLTAGE_SET:
//                if (commandTask != null) {
//                    FarmServer.getThreadPool().submit(new UpperControlFutureTask(new HumSetCallable(commandTask)));
//                }
//                break;
//            case CMD_DEV_SHORTCONN_HUM_SET:
//                if (commandTask != null) {
//                    FarmServer.getThreadPool().submit(new UpperControlFutureTask(new PositionSetCallable(commandTask)));
//                }
//                break;
//            case CMD_DEV_SHORTCONN_LOCATION_SET:
//                logger.debug("离线指令下发完成!!!");
//        }
    }

    public void dealTimeout(CmdResEvent event) {
        //检查是否有离线任务，有任务继续执行任务
//        BuguDao<CommandTask> ctDao = DaoCache.getInstance().get(CommandTask.class);
//        BuguQuery<CommandTask> q1 = ctDao.query().notEquals("tempComplete", Boolean.TRUE);
//        BuguQuery<CommandTask> q2 = ctDao.query().notEquals("positionComplete", Boolean.TRUE);
//        BuguQuery<CommandTask> q3 = ctDao.query().notEquals("humComplete", Boolean.TRUE);
//        CommandTask commandTask = ctDao.query().is("terminalSN", event.getTerminalSN()).or(q1, q2, q3).result();
//        switch (event.getCmd()) {
//            case CMD_DEV_SHORTCONN_TEMP_HUM_VOLTAGE_SET:
//                logger.debug("设备{}温度信息设置指令无响应，重发!!!", event.getTerminalSN());
//                if (commandTask != null) {
//                    FarmServer.getThreadPool().submit(new UpperControlFutureTask(new TempSetCallable(commandTask)));
//                }
//                break;
//            case CMD_DEV_SHORTCONN_HUM_SET:
//                logger.debug("设备{}湿度信息设置指令无响应，重发!!!", event.getTerminalSN());
//                if (commandTask != null) {
//                    FarmServer.getThreadPool().submit(new UpperControlFutureTask(new HumSetCallable(commandTask)));
//                }
//                break;
//            case CMD_DEV_SHORTCONN_LOCATION_SET:
//                logger.debug("设备{}位置信息设置指令无响应，重发!!!", event.getTerminalSN());
//                if (commandTask != null) {
//                    FarmServer.getThreadPool().submit(new UpperControlFutureTask(new PositionSetCallable(commandTask)));
//                }
//        }
    }

    public void dealException(CmdResEvent event) {
        logger.error("{}下发期间发生异常!!!", event.getCmd().getChName());
    }
}
